package com.servlet;

import com.dao.PurchaseDao;
import com.dao.SupplierDao;
import com.dao.ZgoodsDao;
import com.dao.impl.PMationImpl;
import com.dao.impl.PurchaseDaoImpl;
import com.dao.impl.SupplierDaoImpl;
import com.dao.impl.ZgoodDaoImpl;
import com.entry.Purchase;
import com.entry.Supplier;
import com.entry.Zgoods;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import com.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/cg_manage/purchase.do")
public class PurchaseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        Purchase p = new Purchase();
        PurchaseDao pImpl = new PurchaseDaoImpl();
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        if ("find".equals(action)) {
            session.removeAttribute("pess");
            JsonParser jParser = new JsonParser();
            JsonObject jt = null;
            int pageSize = request.getParameter("pageSize") == null ? 5 : Integer.parseInt(request.getParameter("pageSize").toString());
            int pageNo = request.getParameter("pageNo") == null ? 1 : Integer.parseInt(request.getParameter("pageNo").toString());
            //查询条件
            if(request.getParameter("pageNo") == null || request.getParameter("pageSize") == null){
                session.removeAttribute("info");
            }
            if(session.getAttribute("info") == null){
                jt = (JsonObject) jParser.parse(request.getParameter("info"));
            }else{
                jt = (JsonObject) jParser.parse(String.valueOf(session.getAttribute("info")));
            }
            String pname = jt.get("pname").getAsString();
            try {
                float starprice = "".equals(jt.get("starprice").getAsString()) || jt.get("starprice").getAsString() == null? 0 : Float.valueOf(jt.get("starprice").getAsString());
                float endprice = "".equals(jt.get("endprice").getAsString()) || jt.get("endprice").getAsString() == null? 0 : Float.valueOf(jt.get("endprice").getAsString());
                int pcount = "".equals(jt.get("pcount").getAsString()) || jt.get("pcount").getAsString() == null? 0 : Integer.valueOf(jt.get("pcount").getAsString());

                String group = jt.get("group").getAsString();

                JsonObject language1=new JsonObject();
                language1.addProperty("pname", pname);
                language1.addProperty("starprice", String.valueOf(starprice));
                language1.addProperty("endprice", String.valueOf(endprice));
                language1.addProperty("pcount", String.valueOf(pcount));
                language1.addProperty("group", group);
                PageModel<Purchase> pm = pImpl.find(pageSize, pageNo, pname, starprice, endprice, pcount, group);
                pm.setPageNav(request.getRequestURI()+"?action=find&info" + language1);
                session.setAttribute("find", pm);
                System.out.println(pm.getPageNav());
                session.setAttribute("info",language1);
                response.sendRedirect("./cg_find_jinhuo.jsp");
            } catch (NumberFormatException e) {
                session.setAttribute("pess", "单价输入错误");
                response.sendRedirect("./cg_find_jinhuo.jsp");
            }
        }else if("tfind".equals(action)){
            int pageSize = request.getParameter("pageSize") == null ? 5 : Integer.parseInt(request.getParameter("pageSize").toString());
            int pageNo = request.getParameter("pageNo") == null ? 1 : Integer.parseInt(request.getParameter("pageNo").toString());
            if(request.getParameter("pageNo") == null || request.getParameter("pageSize") == null){
                session.removeAttribute("tinfo");
            }
            session.removeAttribute("tpess");
            JsonParser jParser = new JsonParser();
            JsonObject jt = null;
            if(session.getAttribute("tinfo") == null){
                jt = (JsonObject) jParser.parse(request.getParameter("tinfo"));
            }else{
                jt = (JsonObject) jParser.parse(String.valueOf(session.getAttribute("tinfo")));
            }
            //查询条件
            String pname = jt.get("pname").getAsString();
            try {
                float starprice = "".equals(jt.get("starprice").getAsString()) ? 0 : Float.valueOf(jt.get("starprice").getAsString());
                float endprice = "".equals(jt.get("endprice").getAsString()) ? 0 : Float.valueOf(jt.get("endprice").getAsString());
                int pcount = "".equals(jt.get("pcount").getAsString()) ? 0 : Integer.valueOf(jt.get("pcount").getAsString());
                String group = jt.get("group").getAsString();
                JsonObject language1=new JsonObject();
                language1.addProperty("pname", pname);
                language1.addProperty("starprice", String.valueOf(starprice));
                language1.addProperty("endprice", String.valueOf(endprice));
                language1.addProperty("pcount", String.valueOf(pcount));
                language1.addProperty("group", group);
                PageModel<Purchase> pm = pImpl.find(pageSize, pageNo, pname, starprice, endprice, pcount, group);
                pm.setPageNav(request.getRequestURI()+"?action=tfind&tinfo" + language1);
                session.setAttribute("tinfo",language1);
                session.setAttribute("tufind", pm);
                response.sendRedirect("./cg_find_tuihuo.jsp");
            } catch (NumberFormatException e) {
                session.setAttribute("tpess", "单价输入错误");
                response.sendRedirect("./cg_find_tuihuo.jsp");
            }
        } else if ("insert".equals(action)) {
            JsonParser jParser = new JsonParser();
            JsonObject jt = (JsonObject) jParser.parse(request.getParameter("info"));//构造JsonObject对象
            String gid = jt.get("gid").getAsString();
            String pname = jt.get("pname").getAsString();
            double price = jt.get("price").getAsDouble();
            int pcount = jt.get("pcount").getAsInt();
            String ptime = jt.get("ptime").getAsString();
            String group = jt.get("group").getAsString();
            String sup_name = jt.get("sup_name").getAsString();
            p.setGid(gid);
            p.setPname(pname);
            p.setPrice(price);
            p.setPcount(pcount);
            p.setPtime(ptime);
            p.setGroup(group);
            String mess = validateForm(p, sup_name);
            if ("".equals(mess)) {
                int i = pImpl.add(p);
                System.out.println("pIml添加结果" + i);
                if (i > 0) {
                    ZgoodsDao zImpl = new ZgoodDaoImpl();
                    PMationImpl pmationImpl = new PMationImpl();
                    //查询商品是否存在
                    Zgoods z = pmationImpl.findName(pname);
                    System.out.println("Zgoods查询成功");
                    //存在更新
                    if (z != null) {
                        int oldcount = z.getZcount();
                        int num = 0;
                        System.out.println("group值：" + group);
                        if ("进货".equals(group)) {
                            num = 1;
                        } else if ("退货".equals(group)) {
                            num = 2;
                        }
                        int k = zImpl.update(pname, pcount, oldcount, num);
                        System.out.println("zImpl更新:" + k);
                        //不存在添加
                    } else {
                        zImpl.add(pname, pcount, gid, sup_name);
                    }
                    session.setAttribute("Mess", "添加成功！");
                    if ("进货".equals(group)) {
                        response.sendRedirect("./cg_find_jinhuo.jsp");
                    } else if ("退货".equals(group)) {
                        response.sendRedirect("./cg_find_tuihuo.jsp");
                    }
                } else {
                    session.setAttribute("Mess", "添加失败！");
                    response.sendRedirect("./cg_add.jsp");
                }
            } else {
                session.setAttribute("Mess", mess);
                response.sendRedirect("./cg_add.jsp");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    private String validateForm(Purchase pur, String sup_name) {
        PurchaseDao pImpl = new PurchaseDaoImpl();
        SupplierDao sImpl = new SupplierDaoImpl();
        if (sImpl.findId(sup_name) == null) {
            return "供应商不存在";
        }
        if (pImpl.findGid(pur.getGid()) == 0) {
            return "gid不存在！";
        }
        return "";
    }
}
